package com.example.beautymakeup.service.impl;

import com.example.beautymakeup.dao.CartDao;
import com.example.beautymakeup.domain.PageBean;
import com.example.beautymakeup.pojo.Cart;
import com.example.beautymakeup.pojo.Hot;
import com.example.beautymakeup.service.CartService;
import com.example.beautymakeup.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartDao cartDao;

    @Override
    public boolean add(String token, Cart cart) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if (!"admin".equals(subject)){
            cart.setUserId(Integer.valueOf(subject));
            return cartDao.add(cart) == 1;
        }
        return false;
    }

    @Override
    public PageBean<Cart> get(String token) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if (!"admin".equals(subject)){
            Cart cart = new Cart();
            cart.setUserId(Integer.valueOf(subject));
            List<Cart> carts = cartDao.get(cart);
            int count = cartDao.getCount(cart);
            PageBean<Cart> pageBean = new PageBean<>();
            pageBean.setRows(carts);
            pageBean.setTotalCount(count);
            return pageBean;
        }
        return null;
    }

    @Override
    public boolean delete(String token, Cart cart) throws Exception {
        String subject = JwtUtil.parseJWT(token).getSubject();
        if (!"admin".equals(subject)){
            cart.setUserId(Integer.valueOf(subject));
            return cartDao.delete(cart) != 0;
        }
        return false;
    }
}
